package ps.client.util;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.util.Vector;

import ps.client.gui.dpsoverlay.DpsListEntry;
import ps.net.DpsParseContent;


public class ImportDpsLookupThread extends Thread {
    
    private final static String NEW_LINE = "\n";
    private final static String DELIM = ";";
    
    private final static String[] IGNORE_LIST = { "Unbekannt", "Ahnenwachtpo", "Inquisition" };

    boolean running = true;
    
    public ImportDpsLookupThread() {
        super("ImportDpsLookup");
        setDaemon(true);
        
        start();
    }
    
    public void stopThread() {
        running = false;
    }   
    
    @Override
    public void run() {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        
        while (running) {
            try {
                String content = (String)clipboard.getData(DataFlavor.stringFlavor);
                if (content.startsWith("PSPARSE")) {
                    String[] lines = content.split(NEW_LINE);
                    if (lines.length >= 2) {
                        try {
                            String[] titleParts = lines[0].split(DELIM);
                            
                            DpsParseContent dpsParseContent = new DpsParseContent();
                            dpsParseContent.setTitle(titleParts[1]);
                            dpsParseContent.setTime(titleParts[2]);
                            dpsParseContent.setDamage(titleParts[3]);
                            dpsParseContent.setDps(titleParts[4]);
                        
                        
                            int damage = Integer.parseInt(dpsParseContent.getDamage());
                            if (damage > 0) {
                                Vector<DpsListEntry> dpsList = new Vector<DpsListEntry>(30);
                                for (int i = 1; i < lines.length; i++) {
                                    String[] lineParts = lines[i].split(DELIM);
                                    DpsListEntry dpsListEntry = new DpsListEntry();
                                    dpsListEntry.setName(lineParts[0]);
                                    dpsListEntry.setDps(lineParts[1]);
                                    
                                    boolean ignoreEntry = false;
                                    for (String ignoreStr : IGNORE_LIST) {
                                        if (ignoreStr.equalsIgnoreCase(dpsListEntry.getName())) {
                                            ignoreEntry = true;
                                        }
                                    }
                                    if (!ignoreEntry) {
                                        dpsList.add(dpsListEntry);
                                    }
                                }
                                dpsParseContent.setDpsListEntries(dpsList.toArray(new DpsListEntry[dpsList.size()]));
                                
                                fireNewDpsParse(dpsParseContent);
                            }
                        } catch (Exception ex) {
                        }
                    }
                    clipboard.setContents(new StringSelection(""), null);
                }
            } catch (Exception ex) {
            }
            
            try {
                sleep(250);
            } catch (InterruptedException ex) {
                if (running) {
                    ex.printStackTrace();
                }
            }
        }
    }
    
    protected void fireNewDpsParse(DpsParseContent dpsParseContent) {
    }

}
